package main

import (
    "database/sql"
    "fmt"
    "go.uber.org/zap"
    "google.golang.org/protobuf/proto"
    "laxkits/lib/laxmysql"
    "laxkits/lib/laxutils"
    KKSG "laxkits/lib/ropb"
    "laxkits/lib/zlogger"
)

type XMLNode struct {
    Log zlogger.LaxLogNode `xml:"log"`
    Mysql []laxmysql.LaxMySQLConfig `xml:"mysqlgroup>mysql"`
}
var globalXMLConfig XMLNode

func main() {
    if err := laxutils.LoadXmlFile("../eg.xml", &globalXMLConfig); err != nil {
        fmt.Printf("加载xml失败[%v]\n", err)
        return
    }
    fmt.Println(globalXMLConfig)
    zlogger.InitLogger(&globalXMLConfig.Log)
    if err := laxmysql.GetLaxMySQLMgr().RegisterByConfig(globalXMLConfig.Mysql); err != nil {
        zap.S().Errorf("注册mysql失败[%v]", err)
        return
    }
    mysqlConn, err := laxmysql.NewMySQLConn(&globalXMLConfig.Mysql[0])
    if err != nil {
        zap.S().Errorf("连接mysql[%s]失败[%v]", globalXMLConfig.Mysql[0].ToString(), err)
        return
    }
    defer mysqlConn.Close()
    if rowsAffect, err := mysqlConn.Execute("delete from s2 where svrid=?", 801); err != nil {
        zap.S().Warnf("执行1 失败[%v]", err)
    } else {
        zap.S().Infof("执行1，行数[%d]", rowsAffect)
    }
    if rowsAffect, err := mysqlConn.Execute("delete from s2 where svrid=?", 903); err != nil {
        zap.S().Warnf("执行2 失败[%v]", err)
    } else {
        zap.S().Infof("执行2，行数[%d]", rowsAffect)
    }

    if err = mysqlConn.QueryRow("select svrid,user_name,configbranch from s2 where svrid > ?", func(row *sql.Row) error {
        var sid int32
        var uname, cbr string
        if scanerr := row.Scan(&sid, &uname, &cbr); scanerr != nil {
            return scanerr
        }
        zap.S().Infof("查询1成功:%d, %s ,%s ", sid, uname, cbr)
        return nil
    }, 905); err != nil {
        zap.S().Warnf("查询1失败[%v]", err)
    }

    type Node struct {
        sid   int32
        uname string
        cbr   string
    }

    var node []Node

    if err = mysqlConn.Query("select svrid, user_name, configbranch from s1 where svrid between ? and ?", func(rows *sql.Rows) error {
        n := Node{}
        if scanerr := rows.Scan(&n.sid, &n.uname, &n.cbr); scanerr != nil {
            return scanerr
        }
        node = append(node, n)
        return nil
    }, 777,779); err != nil {
        zap.S().Warnf("查询2失败[%v]", err)
    }
    zap.S().Info(node)
    return;
    if err = mysqlConn.QueryRow("select SkillRecord from player0.role where id = ?", func(row *sql.Row) error {
        var arg KKSG.SkillRecord
        var b []byte
        if scanErr := row.Scan(&b); scanErr != nil {
            return scanErr
        }
        if protoErr := proto.Unmarshal(b, &arg); protoErr != nil {
            return protoErr
        }
        zap.S().Info(arg)
        return nil
    }, 51000); err != nil {
        zap.S().Warnf("查询角色失败[%v]", err)
    }
    //laxutils.CloseGracefully(nil)
}
